package com.nulldev.util.concurrency.threadIt.v3.extensions;

import java.util.Map;

import com.nulldev.util.data.Arrays.maps.trie.TrieMap;

public enum TaskStatus {
	SUBMISSION_FAILED("task.submission_failure", -5, true),
	FAILED("task.failed", -4, true),
	CANCELLED("task.cancelled", -3, true),
	KILLED("task.killed", -2, true),
	UNSUBMITTED("task.unsubmitted", -1, false),
	QUEUED("task.queued", 0, false),
	RUNNING("task.running", 1, false),
	COMPLETED("task.completed", 2, true),

	;

	private final int tid;
	private final String tids;
	private final boolean isCompleted;

	private TaskStatus(final String tids, final int tid, final boolean isCompleted) {
		this.tids = tids;
		this.tid = tid;
		this.isCompleted = isCompleted;
	}

	public boolean isCompleted() {
		return this.isCompleted;
	}

	public int getID() {
		return this.tid;
	}

	public String getStringID() {
		return this.tids;
	}

	public static TaskStatus from(final int i) {
		for (final TaskStatus ts : values()) {
			if (ts.tid == i)
				return ts;
		}
		return UNSUBMITTED;
	}

	private static final Map<Integer, TaskStatus> stat_map;
	static {
		final TaskStatus[] vx = values();
		stat_map = new TrieMap<Integer, TaskStatus>();
		for (final TaskStatus v : vx)
			stat_map.putIfAbsent(v.tid, v);
	}

	public static TaskStatus read(final int i) {
		return stat_map.get(i);
	}
}
